<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/13
 * Time: 13:43
 */
namespace app\api\controller;
use think\Db;
use think\Request;
use app\common\model\JlouOrderModel as jl;
use app\common\model\LogOrderModel as lo;
class Vote extends Nocommon
{
    /*
     * 投票列表
     */
    public function vote_list()
    {
        if (Request::instance()->isPost()) {
            $page = $this->p();
            $res = Db::name('vote')->alias('v')->field('v.id,v.title,v.image,v.pass_time,u.nickname,u.user_icon')
                ->join('user u', 'v.uid=u.uid', 'left')
                ->where('is_pass', 1)
                ->where('v.is_del', 0)
                ->where('v.is_display',1)
                ->where('v.end_time','>',date('Y-m-d H:i:s'))
                ->limit(($page - 1) * config('page_size'), config('page_size'))
                ->order('v.id','desc')
                ->select();
            if ($res) {
                foreach ($res as $k => $v) {
                    $res[$k]['pass_time'] = time_ago($v['pass_time']);
                    $data= Db::name('vote_option')->where('vote_id',$v['id'])
                        ->field('id,vote_title,vote_image,vote_number')
                        ->select();
                    foreach($data as $key=>$value){
                        if(empty($value['vote_image'])){
                            $res[$k]['is_image'] = 1;
                        }else{
                            $res[$k]['is_image'] =2;
                        }
                    }
                }

            }

            $this->apiReturn('200', '成功', $res);

        }
    }

    /*
     * 发起投票
     */
    public function up_vote()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $data = [
                'uid'=>$post['uid'],
                'title'=>$post['title'],
                'image'=>$post['image'],
                'content'=>$post['content'],
                'create_time'=>date('Y-m-d H:i:s'),
                'end_time'=>$post['end_time'],
            ];
            $res = Db::name('vote')->insertGetId($data);
            if(!empty($res)){
                $option = json_decode($post['option'],'true');
                $arr = [
                    'vote_id'=>$res
                ];
                array_walk($option, function (&$value, $key, $arr) {
                    $value = array_merge($value, $arr);
                }, $arr);
                $op = Db::name('vote_option')->insertAll($option);
                if($op){
                    $this->apiReturn('200','发起成功');
                }else{
                    $this->apiReturn('400','发起失败');
                }
            }else{
                $this->apiReturn('400','发起失败');
            }

        }
    }

    /*
     * 投票详情
     */
    public function vote_details()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $data= Db::name('vote')->alias('v')->field('v.id as vid,v.title,v.image,v.end_time,v.content,v.pass_time,u.nickname,u.user_icon')
                ->join('user u', 'v.uid=u.uid', 'left')
                ->where('is_pass', 1)
                ->where('v.is_del', 0)
                ->where('v.id',$post['id'])
                ->find();
            $data['option']= Db::name('vote_option')->where('vote_id',$post['id'])
                ->field('id,vote_title,vote_image,vote_number')
                ->order('vote_number','desc')
                ->select();
            foreach($data['option'] as $k=>$v){
                if(empty($v['vote_image'])){
                    $data['type'] = 1;
                }else{
                    $data['type'] =2;
                }
            }
            $data['pass_time'] = time_ago($data['pass_time']);

            $this->apiReturn('200','成功',$data);
        }
    }
    /*
     * 为她投票
     */
    public function prize()
    {
        if(Request::instance()->isPost()){
            $arr = [
                'id'=>'-1',
                'prize_name'=>'赞',
                'prize_image'=>'http://qiniu.lancly.com/zan.png',
                'prize_price'=>'免费',
                'prize_number'=>1,
            ];

            $res = Db::name('vote_prize')->field('id,prize_name,prize_image,prize_price,prize_number')->where('is_display',1)->where('is_del',0)->select();
            array_unshift($res,$arr);
            $this->apiReturn('200','成功',$res);
        }
    }

    /*
     * 确认投票
     */
    public function yes_vote()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            //查询是否已经该这个选项投过票了
            $res = Db::name('vote_free')->where('option_id',$post['option_id'])->where('uid',$post['uid'])->find();
            if(empty($res)){
                $data = [
                    'option_id'=>$post['option_id'],
                    'uid'=>$post['uid'],
                ];
                $insert = Db::name('vote_free')->insertGetId($data);
                if($insert){
                    //让数自增
                    Db::name('vote_option')->where('id',$post['option_id'])->setInc('vote_number');
                    $this->apiReturn('200','投票成功');

                }else{
                    $this->apiReturn('400','投票失败');
                }
            }else{
                $this->apiReturn('400','免费投票已经在该选项使用');
            }

        }
    }

    /*
     * 支付
     */
    public function order()
    {
        if(Request::instance()->isPost()){
            $post = Request::instance()->post();
            $option_id = $post['option_id'];
            $payType = $post['pay_type'];// 1支付宝 2微信
            $uid = $post['uid'];//用户id
            $prize_id = $post['prize_id'];
            //查询是哪个投票id
            $vote_id = Db::name('vote_option')->field('vote_id')->where('id',$option_id)->find();
            if(!empty($vote_id)){
                //查看是否投票时间已过
                $time = Db::name('vote')->field('end_time')->where('id',$vote_id['vote_id'])->find();

                if($time){
                    if(time()>strtotime($time['end_time'])){
                        $this->apiReturn('400','投票已过期');
                    }
                }

            }
            //查询商品的信息
            $info = Db::name('vote_prize')->field('prize_name,prize_price,prize_number')->where('id',$prize_id)->find();
            if(empty($info)){
                $this->apiReturn('401','奖品信息为空');
            }
            Db::startTrans();
            try{
                $data = [
                    'order_sn'=>makeOrderSn(),
                    'shop_name'=>$info['prize_name'],
                    'vote_num'=>$info['prize_number'],
                    'order_status'=>0,
                    'order_price'=>$info['prize_price'],
                    'uid'=>$uid,
                    'option_id'=>$option_id,
                    'pay_type'=>$payType,
                    'create_time'=>date('Y-m-d H:i:s'),
                ];
                $result = Db::name('vote_order')->insertGetId($data);
                if(!empty($result)){
                    $jl = new jl();
                    $logOrder = new lo();
                    $logOrder->addLogOrder($result,6, $uid, 0, '下单成功，等待用户支付');
                    //查询订单编号
                    $order_sn = Db::name('vote_order')->field('order_sn')->where('order_id',$result)->find();
                    if(empty($order_sn)){
                        $this->apiReturn('402','订单编号错误');
                    }
                    //吊起支付宝或是微信支付
                    if($payType == 1){
                        //调起支付宝接口
                        $data = $jl->getAlipays($order_sn['order_sn'],$uid,6,$info['prize_price']);
                    }
                    if($payType ==2){
                        //调起微信支付
                        $data = $jl->getWxpays($order_sn['order_sn'],6,$uid,$info['prize_price']);
                    }
                }
                Db::commit();
                $this->apiReturn('200','提交订单成功',$data);
            }catch (\Exception $e){
                Db::rollback();
                return apiReturn('400', $e->getMessage(),0);
            }

        }
    }
}